<template>
  <div class="app-container">
    <div class="" style="width:100%;padding:10px;height:50px">
      <el-button size="mini" type="primary" style="float:left" @click="handleCreate()">新增</el-button>
      <el-button
        size="mini"
        type="primary"
        style="float: right; margin-left: 10px"
        icon="el-icon-search"
        @click="handleFilter"
        >搜索
      </el-button>
      <el-input
            v-model="listQuery.contractName_LIKE"
            autocomplete="off"
            placeholder="请输入合约名称"
        style="float: right; width: 150px;margin-left: 5px;"
          />
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      :header-cell-style="{'background-color': '#F5F8FB'}"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column align="center" label="序号" type="index" :index="indexMethod" />
      <!--<el-table-column align="center" label="合约ID">
        <template slot-scope="scope">
          <span :title="scope.row.contractId">{{ scope.row.contractId }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="企业ID">
        <template slot-scope="scope">
          <span :title="scope.row.businessId">{{ scope.row.businessId }}</span>
        </template>
      </el-table-column>-->
      <el-table-column align="center" label="合约名称">
        <template slot-scope="scope">
          <span :title="scope.row.contractName">{{ scope.row.contractName }}</span>
        </template>
      </el-table-column>
      <!--<el-table-column align="center" label="签约用户ID">
        <template slot-scope="scope">
          <span :title="scope.row.userId">{{ scope.row.userId }}</span>
        </template>
      </el-table-column>-->
      <el-table-column align="center" label="签署时间">
        <template slot-scope="scope">
          <span :title="scope.row.signingDate.substring(0,10)">{{ scope.row.signingDate.substring(0,10) }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="合约开始日期">
        <template slot-scope="scope">
          <span :title="scope.row.startTime.substring(0,10)">{{ scope.row.startTime.substring(0,10) }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="合约结束日期">
        <template slot-scope="scope">
          <span :title="scope.row.endTime.substring(0,10)">{{ scope.row.endTime.substring(0,10) }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="用户数量">
        <template slot-scope="scope">
          <span :title="scope.row.userNum">{{ scope.row.userNum }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="系统数量">
        <template slot-scope="scope">
          <span :title="scope.row.systemNum">{{ scope.row.systemNum }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="状态">
        <template slot-scope="scope">
          <span :title="scope.row.status==0?'待确认':(scope.row.status==1?'正常':(scope.row.status==2?'已过期':'无效'))">{{ scope.row.status==0?'待确认':(scope.row.status==1?'正常':(scope.row.status==2?'已过期':'无效')) }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="支付方式">
        <template slot-scope="scope">
          <span :title="scope.row.payType==1?'微信':'支付宝'">{{ scope.row.payType==1?'微信':'支付宝' }}</span>
        </template>
      </el-table-column>
      <!--<el-table-column align="center" label="创建人">
        <template slot-scope="scope">
          <span :title="scope.row.createId">{{ scope.row.createId }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="创建时间">
        <template slot-scope="scope">
          <span :title="scope.row.createTime">{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="更新人">
        <template slot-scope="scope">
          <span :title="scope.row.updateId">{{ scope.row.updateId }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="更新时间">
        <template slot-scope="scope">
          <span :title="scope.row.updateTime">{{ scope.row.updateTime }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="删除状态(0-正常,1-已删除)">
        <template slot-scope="scope">
          <span :title="scope.row.delFlag">{{ scope.row.delFlag }}</span>
        </template>
      </el-table-column>-->
      <el-table-column fixed="right" align="center" label="操作" class-name="small-padding fixed-width" width="250px">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="success" @click="handleConfirm(scope.row)">确认</el-button>
          <el-button size="mini" type="warning" @click="handleRefuse(scope.row)">拒绝</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
        :current-page="listQuery.page"
        :page-sizes="[10,20,30,50]"
        :page-size="listQuery.limit"
        :total="total"
        background
        layout="total, sizes, prev, pager, next"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <el-dialog title="合约编辑" :visible.sync="dialogVisible">
      <el-form ref="contractForm" :model="contract" :rules="rules" size="mini">
        <!--<el-form-item label="合约ID" prop="contractId" :label-width="formLabelWidth">
          <el-input v-model="contract.contractId" autocomplete="off" />
        </el-form-item>
        <el-form-item label="企业ID" prop="businessId" :label-width="formLabelWidth">
          <el-input v-model="contract.businessId" autocomplete="off" />
        </el-form-item>-->
        <el-form-item label="合约名称" prop="contractName" :label-width="formLabelWidth">
          <el-input v-model="contract.contractName" autocomplete="off" />
        </el-form-item>
        <!--<el-form-item label="签约用户ID" prop="userId" :label-width="formLabelWidth">
          <el-input v-model="contract.userId" autocomplete="off" />
        </el-form-item>-->
        <el-form-item label="签署时间" prop="signingDate" :label-width="formLabelWidth">
          <!--<el-input v-model="contract.signingDate" autocomplete="off" />-->
          <el-date-picker
            v-model="contract.signingDate"
            type="date"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="生效时间" prop="date" :label-width="formLabelWidth">
          <!--<el-input v-model="contract.date" autocomplete="off" />-->
          <el-date-picker
            v-model="contract.date"
            type="daterange"
            value-format="yyyy-MM-dd HH:mm:ss"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <!--<el-form-item label="合约开始日期" prop="startTime" :label-width="formLabelWidth">
          <el-input v-model="contract.startTime" autocomplete="off" />
        </el-form-item>
        <el-form-item label="合约结束日期" prop="endTime" :label-width="formLabelWidth">
          <el-input v-model="contract.endTime" autocomplete="off" />
        </el-form-item>-->
        <el-form-item label="用户数量" prop="userNum" :label-width="formLabelWidth">
          <el-input v-model="contract.userNum" autocomplete="off" />
        </el-form-item>
        <el-form-item label="系统数量" prop="systemNum" :label-width="formLabelWidth">
          <el-input v-model="contract.systemNum" autocomplete="off" />
        </el-form-item>
        <!--<el-form-item label="状态" prop="status" :label-width="formLabelWidth">
          <el-input v-model="contract.status" autocomplete="off" />
        </el-form-item>-->
        <el-form-item label="支付方式" prop="payType" :label-width="formLabelWidth">
          <!--<el-input v-model="contract.payType" autocomplete="off" />-->
          <el-radio-group v-model="contract.payType">
            <el-radio :label="1">微信</el-radio>
            <el-radio :label="2" >支付宝</el-radio>
          </el-radio-group>
        </el-form-item>
        <!--<el-form-item label="创建人" prop="createId" :label-width="formLabelWidth">
          <el-input v-model="contract.createId" autocomplete="off" />
        </el-form-item>
        <el-form-item label="创建时间" prop="createTime" :label-width="formLabelWidth">
          <el-input v-model="contract.createTime" autocomplete="off" />
        </el-form-item>
        <el-form-item label="更新人" prop="updateId" :label-width="formLabelWidth">
          <el-input v-model="contract.updateId" autocomplete="off" />
        </el-form-item>
        <el-form-item label="更新时间" prop="updateTime" :label-width="formLabelWidth">
          <el-input v-model="contract.updateTime" autocomplete="off" />
        </el-form-item>
        <el-form-item label="删除状态(0-正常,1-已删除)" prop="delFlag" :label-width="formLabelWidth">
          <el-input v-model="contract.delFlag" autocomplete="off" />
        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" size="mini" @click="handleSaveOrUpdate()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { del, get, getPage, save, update } from '@/api/Contract'

export default {
  data() {
    return {
      total: null,
      list: null,
      listLoading: true,
      dialogVisible: false,
      listQuery: {
        page: 1,
        size: 10
      },
      contract: {
        contractId: null,
        businessId: null,
        contractName: null,
        userId: null,
        signingDate: null,
        date:null,
        startTime: null,
        endTime: null,
        userNum: null,
        systemNum: null,
        status: null,
        payType: null,
        createId: null,
        createTime: null,
        updateId: null,
        updateTime: null,
        delFlag: null,
      },
      contractCopy: null,
      rules: {
        contractId: [{ required: true, message: '请填写合约ID', trigger: 'blur' }],
        businessId: [{ required: true, message: '请填写企业ID', trigger: 'blur' }],
        contractName: [{ required: true, message: '请填写合约名称', trigger: 'blur' }],
        userId: [{ required: true, message: '请填写签约用户ID', trigger: 'blur' }],
        signingDate: [{ required: true, message: '请填写签署时间', trigger: 'blur' }],
        date: [{ required: true, message: '请填写生效时间', trigger: 'blur' }],
        //startTime: [{ required: true, message: '请填写合约开始日期', trigger: 'blur' }],
        //endTime: [{ required: true, message: '请填写合约结束日期', trigger: 'blur' }],
        userNum: [{ required: true, message: '请填写用户数量', trigger: 'blur' }],
        systemNum: [{ required: true, message: '请填写系统数量', trigger: 'blur' }],
        status: [{ required: true, message: '请填写状态', trigger: 'blur' }],
        payType: [{ required: true, message: '请填写支付方式', trigger: 'blur' }],
        createId: [{ required: true, message: '请填写创建人', trigger: 'blur' }],
        createTime: [{ required: true, message: '请填写创建时间', trigger: 'blur' }],
        updateId: [{ required: true, message: '请填写更新人', trigger: 'blur' }],
        updateTime: [{ required: true, message: '请填写更新时间', trigger: 'blur' }],
        delFlag: [{ required: true, message: '请填写删除状态(0-正常,1-已删除)', trigger: 'blur' }],
      },
      formLabelWidth: '120px'
    }
  },
  created() {
    this.getPage()
  },
  methods: {
    getPage() {
      this.listLoading = true
      getPage(this.listQuery).then(response => {
        this.list = response.data.records
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleSizeChange(val) {
      this.listQuery.size = val
      this.getPage()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getPage()
    },
    indexMethod(index) {
      return this.listQuery.size * (this.listQuery.page - 1) + index + 1
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getPage()
    },
    handleCreate() {
      Object.keys(this.contract).forEach(key => (this.contract[key] = null))
      this.contractCopy = null
      this.dialogVisible = true
      if (this.$refs['contractForm'] !== undefined) {
        this.$refs['contractForm'].clearValidate()
      }
    },
    handleConfirm(row) {
      this.$confirm('是否确认该合约?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        row.status = 1
        edit(row).then(() => {
          this.$message({
            type: 'success',
            message: '确认成功'
          })
          this.getPage()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消确认'
        })
      })
    },
    handleRefuse(row) {
      this.$confirm('是否拒绝该合约?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        row.status = 3
        edit(row).then(() => {
          this.$message({
            type: 'success',
            message: '拒绝成功'
          })
          this.getPage()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消拒绝'
        })
      })
    },
    handleEdit(row) {
      const params = { id: row.contractId }
      get(params).then(response => {
        this.contract = response.data
        this.contract.date = [];
        this.contract.date.push(this.contract.startTime);
        this.contract.date.push(this.contract.endTime);
        this.contractCopy = response.data
      })
      this.dialogVisible = true
      if (this.$refs['contractForm'] !== undefined) {
        this.$refs['contractForm'].clearValidate()
      }
    },
    handleDelete(row) {
      this.$confirm('是否删除该数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        del(row.contractId).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功'
          })
          this.getPage()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消删除'
        })
      })
    },
    handleSaveOrUpdate() {
      this.$refs['contractForm'].validate((valid) => {
        if (valid) {
          this.contract.startTime = this.contract.date[0]
          this.contract.endTime = this.contract.date[1]
          if (this.contract.contractId) {
            update(this.contract).then(() => {
              this.$message({
                type: 'success',
                message: '更新成功'
              })
              this.dialogVisible = false
              this.getPage()
            })
          } else {
            save(this.contract).then(() => {
              this.$message({
                type: 'success',
                message: '保存成功'
              })
              this.dialogVisible = false
              this.getPage()
            })
          }
        } else {
          this.$message.error('请保证数据填写正确！')
        }
      })
    }
  }
}
</script>

<style>
</style>
